package com.shopping.mall.core.module.type.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.type.entity.Type;
import com.shopping.mall.core.module.type.entity.TypeIds;
import com.shopping.mall.core.module.type.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "类别管理")
@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @UnLogin
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<Type>> getTypePage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        Type type) {
        IPage<Type> typePage=typeService.getTypePage(current,size,type);
        return Result.OK(typePage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<Type> update(@Validated(Type.update.class) @RequestBody Type type) {
        typeService.updateById(type);
        return Result.OK();
    }

    @UnLogin
    @ApiOperation("新增")
    @PostMapping
    public Result<Object> add(@Validated(Type.insert.class) @RequestBody Type type) {
        return typeService.add(type);
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<Type> getById(@RequestParam String id){
        Type type=typeService.getById(id);
        return Result.OK(type);
    }

    @UnLogin
    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<Type>> getAll(Type type){
        List<Type> typeList=typeService.getAll(type);
        System.out.println(typeList);
        return Result.OK(typeList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return typeService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return typeService.batchDelete(ids);
    }

    @UnLogin
    @ApiOperation("树型结构查询所有")
    @GetMapping("/tree")
    public Result<List<Type>> getTree(){
        return typeService.getTree();
    }

    @ApiOperation("排序")
    @PostMapping("/order")
    public Result<Object> order(@RequestBody TypeIds typeIds){
        typeService.order(typeIds);
        return Result.OK();
    }
}
